#coding=utf-8
from framework.db.mysql import BaseDb
from tornado import gen
from lib import log


class Outlog(BaseDb):
    @gen.coroutine
    def list(self,start,end):
        sql='''SELECT Fid,Fuser_id,Ftel,Fname,Ftext,Fprice,Fdorm_id,
                Froom_id,Fschool_id,Fstatus
                , Fcreate_time, Faddress, Fpay, Finfo 
                FROM tb_food_log 
                LIMIT %s, %s'''

        countsql='SELECT count(Fid) as count FROM tb_food_log '

        try:
            ret = yield self.executejson(sql, (start, end))
            
            count=yield self.executejson(countsql)
            resp = {'ret': ret,'count':count[0]}
        except Exception, e:
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)

    @gen.coroutine
    def listsr(self,start,end,Fname,Ftel,Fstatus):
            sql='''SELECT Fid,Fuser_id,Ftel,Fname,Ftext,Fprice,Fdorm_id,
                    Froom_id,Fschool_id,Fstatus
                    , Fcreate_time, Faddress, Fpay, Finfo 
                    FROM tb_food_log WHERE Fname like %s AND Ftel like %s AND Fstatus = %s 
                    LIMIT %s, %s'''
            countsql='SELECT count(Fid) as count FROM tb_food_log WHERE Fname like %s AND Ftel like %s AND Fstatus = %s'

            try:
                ret = yield self.executejson(sql, ('%%%s%%' % Fname,'%%%s%%' % Ftel,Fstatus,start, end))
            
                count=yield self.executejson(countsql,('%%%s%%' % Fname,'%%%s%%' % Ftel,Fstatus))
                resp = {'ret': ret,'count':count[0]}
            except Exception, e:
                log.log_exception(self.__class__.__name__, e)
                resp = '-1'
            raise gen.Return(resp)

    #处理状态
    @gen.coroutine
    def status(self,Fid,Fstatus):
        sql='UPDATE tb_food_log SET Fstatus=%s WHERE Fid=%s'
        try:
            res=yield self.upsert(sql,(Fstatus,Fid))
 
        except Exception,e:
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(res)

    #批量处理状态
    @gen.coroutine
    def set_istatus2(self, Fstatus,tuple2):
        sql = 'UPDATE tb_food_log SET Fstatus = %s WHERE Fid in %s'
        
        ret = yield self.upsert_commit(sql, (Fstatus, tuple2))

        raise gen.Return(ret)





